#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include "myOpenCVLIB\myheader.h"

using namespace std;  
using namespace cv; 

void showImg(Mat img, char * name){
	namedWindow(name);
	imshow(name,img);
	waitKey();
}
int main(){

	Mat src, dst, bHist, rHist, gHist;
	int histSize = 256;

	float histRange[] = {0,256};
	const float * pHistRange = { histRange };
	bool uniform = true;
	bool acumulate = false;

	int histWidth =512;
	int histHeight = 400;
	int binW = cvRound((double) histWidth / histSize);


	src = imread(LENA);

	vector<Mat> bgrPlanes;

	split(src, bgrPlanes);

	calcHist(&src,1,0,Mat(),bHist,3,&histSize,&pHistRange,uniform,true);
	/*calcHist(&bgrPlanes[1],1,0,Mat(),gHist,1,&histSize,&pHistRange,uniform,acumulate);
	calcHist(&bgrPlanes[2],1,0,Mat(),rHist,1,&histSize,&pHistRange,uniform,acumulate);*/

	showImg(bHist,"bHist");
	/*showImg(gHist,"gHist");
	showImg(rHist,"rHist");*/
	
	Mat histImage (histHeight,histWidth,CV_8UC3, Scalar(0,0,0));

	normalize(bHist, bHist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
	normalize(gHist, gHist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
	normalize(rHist, rHist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

	for(int i=1;i<histSize;i++){
	
		line(histImage
			,Point(binW*(i-1),histHeight - cvRound(bHist.at<float>(i-1)))
			,Point(binW * i, histHeight-cvRound(bHist.at<float>(i)))
			,Scalar(255,0,0),2,8,0);

		line(histImage
			,Point(binW*(i-1),histHeight - cvRound(gHist.at<float>(i-1)))
			,Point(binW * i, histHeight-cvRound(gHist.at<float>(i)))
			,Scalar(0,255,0),2,8,0);

		line(histImage
			,Point(binW*(i-1),histHeight - cvRound(rHist.at<float>(i-1)))
			,Point(binW * i, histHeight-cvRound(rHist.at<float>(i)))
			,Scalar(0,0,255),2,8,0);
	}

	showImg(histImage,"histImage");


	return 0;
}